package com.codeczx.viewdrawdemo;

import android.content.Context;
import android.icu.util.Measure;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by codeczx on 2017/5/17.
 */

public class MyView extends View {

    private static final String TAG = "MyView";

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureViewWidth(widthMeasureSpec);
        int height = measureViewHeight(heightMeasureSpec);
        Log.i(TAG, "onMeasure: getMeasureWidth():"+getMeasuredWidth());
        Log.i(TAG, "onMeasure: getMeasuredHeight():"+getMeasuredHeight());
        setMeasuredDimension(width,height);
        Log.i(TAG, "onMeasure: after setMeasureDimension()");
        Log.i(TAG, "onMeasure: getMeasureWidth():"+getMeasuredWidth());
        Log.i(TAG, "onMeasure: getMeasuredHeight():"+getMeasuredHeight());
        Log.i(TAG, "onMeasure: getWidth():"+getWidth());
        Log.i(TAG, "onMeasure: getHeight():"+getHeight());
        Log.i(TAG, "onMeasure: ==========================================");
    }

    private int measureViewWidth(int widthMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        Log.i(TAG, "measureViewWidth: specSize width"+specSize);
        if(specMode == MeasureSpec.EXACTLY){
            result = specSize;
        }else{
            result = 600;
            if(specMode == MeasureSpec.AT_MOST){
                result = Math.min(result,specSize);
            }
        }
        return result;
    }

    private int measureViewHeight(int heightMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        Log.i(TAG, "measureViewHeight: specSize height"+specSize);
        if(specMode == MeasureSpec.EXACTLY){
            result = specSize;
        }else{
            result = 600;
            if(specMode == MeasureSpec.AT_MOST){
                result = Math.min(result,specSize);
            }
        }
        return result;
    }
}
